package com.common.base.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.SeekBar
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：15/2/2023
 *
 * 描述：动画工具
 *
 * 修订历史：
 *
 */

object AnimationUtils {

    private var shortAnimationDuration: Long = 0

    /**
     * 从控件所在位置移动到控件的底部
     *
     * @return
     */
    fun moveToViewBottom(): TranslateAnimation {
        val mHiddenAction = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, 1.0f
        )
        mHiddenAction.duration = 500
        return mHiddenAction
    }

    /**
     * 从控件所在位置移动到控件的顶部
     *
     * @return
     */
    fun moveToViewTop(): TranslateAnimation {
        val mHiddenAction = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, -1.0f
        )
        mHiddenAction.duration = 500
        return mHiddenAction
    }

    /**
     * 从控件的底部移动到控件所在位置
     *
     * @return
     */
    fun moveBottomToViewLocation(): TranslateAnimation {
        val mHiddenAction = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            1.0f, Animation.RELATIVE_TO_SELF, 0.0f
        )
        mHiddenAction.duration = 500
        return mHiddenAction
    }

    /**
     * 从控件的顶部移动到控件所在位置
     *
     * @return
     */
    fun moveTopToViewLocation(): TranslateAnimation {
        val mHiddenAction = TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
        )
        mHiddenAction.duration = 500
        return mHiddenAction
    }


    /**
     * 动画显示SeekBar
     */
    fun setSlideBarShow(slideBar: SeekBar?) {

        slideBar?.apply {

            visibility = View.VISIBLE
            //长动画的持续时间为100ms
            shortAnimationDuration = 100

            if (animation != null) {
                animation.cancel()
            }

            //将内容视图设置为100%不透明，并清除视图上的任何动画监听器
            animate()
                .alpha(1f)
                .setDuration(shortAnimationDuration)
                .setListener(null)
        }
    }

    /**
     * 动画隐藏SeekBar
     */
    fun setSlideBarHide(slideBar: SeekBar?) {

        //将动画视图设置为0%不透明，动画结束后，将视图隐藏
        //长动画的持续时间为500ms
        shortAnimationDuration = 500
        slideBar?.apply {
            if (animation != null) {
                animation.cancel()
            }
            animate()
                .alpha(0f)
                .setDuration(shortAnimationDuration)
                .setListener(object : AnimatorListenerAdapter() {
                    @Override
                    override fun onAnimationEnd(animation: Animator) {
                        super.onAnimationEnd(animation)
                        visibility = View.GONE
                    }
                })
        }
    }

    /**
     * RecyclerView绑定SeekBar
     */
    fun setSlideBar(recyclerView: RecyclerView?, slideBar: SeekBar?) {
        slideBar?.let {
            it.setPadding(0, 0, 0, 0)
            it.thumbOffset = 0
        }
        recyclerView?.apply {
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    //显示区域的高度。
                    val extent: Int = computeHorizontalScrollExtent()
                    //整体的高度，注意是整体，包括在显示区域之外的。
                    val range: Int = computeHorizontalScrollRange()
                    //已经向下滚动的距离，为0时表示已处于顶部。
                    val offset: Int = computeHorizontalScrollOffset()
//                    Log.i("dx------", "$range****$extent****$offset")
                    //此处获取seekbar的getThumb，就是可以滑动的小的滚动游标
//                    val gradientDrawable: GradientDrawable = mViewBinding.slideIndicatorPoint.thumb as GradientDrawable
//                    //根据列表的个数，动态设置游标的大小，设置游标的时候，progress进度的颜色设置为和seekbar的颜色设置的一样的，所以就不显示进度了。
//                    gradientDrawable.setSize(extent / ((topTabAdapter?.data?.size ?:0) / 2), 5)
                    //设置可滚动区域
                    slideBar?.max = (range - extent)
                    if (dx == 0) {
                        slideBar?.progress = 0
                    } else if (dx > 0) {
//                        Log.i("dx------", "右滑")
                        slideBar?.progress = offset
                    } else if (dx < 0) {
//                        Log.i("dx------", "左滑")
                        slideBar?.progress = offset
                    }
                }

                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    super.onScrollStateChanged(recyclerView, newState)
                    when (newState) {
                        /*正在拖拽*/
                        RecyclerView.SCROLL_STATE_DRAGGING -> {
                            setSlideBarShow(slideBar)
                        }
                        /*滑动停止*/
                        RecyclerView.SCROLL_STATE_IDLE -> {
                            setSlideBarHide(slideBar)
                        }
                        /*惯性滑动中*/
                        RecyclerView.SCROLL_STATE_SETTLING -> {}
                    }
                }
            })
        }
    }
}